package vsvm.gui.forms;

import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;

import vsvm.data.filter.ranking.LaplacianScore;

import java.awt.FlowLayout;

public class LaplacianForm extends JDialog {

	private static final long serialVersionUID = 100L;
	private JPanel jContentPane = null;
	private JLabel lblEpsilon = null;
	private JLabel lblNearestneighbor = null;
	private JLabel lblConstT = null;
	private JTextField txtEpsilon = null;
	private JTextField txtNearestNeighbor = null;
	private JTextField txtT = null;
	private JRadioButton radiobtnEpsilon = null;
	private JRadioButton radiobtnKnn = null;
	private JButton btnOk = null;
	private JPanel pnlButtons = null;
	private JButton btnCancel = null;
	private JPanel pnlMain = null;
	
	private LaplacianForm me;	
	private LaplacianScore laplacian;
	private JRadioButton radiobtnLabel = null;
	private JLabel jLabel = null;
	
	public LaplacianForm(Frame parent, LaplacianScore l) {
		super(parent);
		this.me = this;
		laplacian = l;
		initialize();
		setLocationRelativeTo(parent);
		this.setVisible(true);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(291, 239);
		this.setTitle("Laplacian Score Form");
		this.setContentPane(getPnlMain());
		this.setContentPane(getPnlMain());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 1;
			gridBagConstraints21.insets = new Insets(0, 10, 5, 0);
			gridBagConstraints21.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints21.gridy = 3;
			jLabel = new JLabel();
			jLabel.setText("Use label information");
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints11.gridy = 3;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints7.gridy = 2;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints6.gridy = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.BOTH;
			gridBagConstraints5.gridy = 4;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.insets = new Insets(5, 10, 0, 0);
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.ipadx = 5;
			gridBagConstraints5.gridx = 2;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.BOTH;
			gridBagConstraints4.gridy = 2;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.insets = new Insets(5, 10, 10, 0);
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.ipadx = 33;
			gridBagConstraints4.gridx = 2;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.gridy = 1;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.insets = new Insets(0, 10, 5, 0);
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.ipadx = 40;
			gridBagConstraints3.gridx = 2;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 1;
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.insets = new Insets(5, 10, 0, 0);
			gridBagConstraints2.gridy = 4;
			lblConstT = new JLabel();
			lblConstT.setText("Similarity constant t:");
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 1;
			gridBagConstraints.insets = new Insets(5, 10, 5, 0);
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.gridy = 2;
			lblNearestneighbor = new JLabel();
			lblNearestneighbor.setText("k nearest neighbours:");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.insets = new Insets(0, 10, 5, 0);
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridy = 1;
			lblEpsilon = new JLabel();
			lblEpsilon.setText("Epsilon-neighbourhoods:");
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(getRadiobtnEpsilon(), gridBagConstraints6);
			jContentPane.add(getRadiobtnKnn(), gridBagConstraints7);
			jContentPane.setBorder(BorderFactory.createTitledBorder(null, "Laplacian score", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			jContentPane.add(lblEpsilon, gridBagConstraints1);
			jContentPane.add(lblNearestneighbor, gridBagConstraints);
			jContentPane.add(lblConstT, gridBagConstraints2);
			jContentPane.add(getTxtEpsilon(), gridBagConstraints3);
			jContentPane.add(getTxtNearestNeighbor(), gridBagConstraints4);
			jContentPane.add(getTxtT(), gridBagConstraints5);
			jContentPane.add(getRadiobtnLabel(), gridBagConstraints11);
			jContentPane.add(jLabel, gridBagConstraints21);
		}
		return jContentPane;
	}

	/**
	 * This method initializes txtEpsilon	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtEpsilon() {
		if (txtEpsilon == null) {
			txtEpsilon = new JTextField();
			if((laplacian.getOption()).equals(LaplacianScore.epsilon))
			{
				txtEpsilon.setText(String.valueOf(laplacian.getEps()));
			}
			else
			{
				txtEpsilon.setEnabled(false);	
			}
			txtEpsilon.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					txtEpsilon.selectAll();
				}
			});
		}
		return txtEpsilon;
	}

	/**
	 * This method initializes txtNearestNeighbor	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtNearestNeighbor() {
		if (txtNearestNeighbor == null) {
			txtNearestNeighbor = new JTextField();
			if((laplacian.getOption()).equals(LaplacianScore.nearestNeighbor))
			{
				txtNearestNeighbor.setText(String.valueOf(laplacian.getKnn()));
			}
			else
			{
				txtNearestNeighbor.setEnabled(false);	
			}
			txtNearestNeighbor.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					txtNearestNeighbor.selectAll();
				}
			});
		}
		return txtNearestNeighbor;
	}

	/**
	 * This method initializes txtT	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtT() {
		if (txtT == null) {
			txtT = new JTextField();
			if(laplacian.getT() != 0)
			{
				txtT.setText(String.valueOf(laplacian.getT()));
			}
			else
			{
				txtT.setText("infinity");
			}
			txtT.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					txtT.selectAll();
				}
			});
		}
		return txtT;
	}

	/**
	 * This method initializes radiobtnEpsilon	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getRadiobtnEpsilon() {
		if (radiobtnEpsilon == null) {
			radiobtnEpsilon = new JRadioButton();
			
			if((laplacian.getOption()).equals(LaplacianScore.epsilon))
			{
				radiobtnEpsilon.setSelected(true);
			}
			radiobtnEpsilon.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					radiobtnEpsilon.setSelected(true);
					radiobtnKnn.setSelected(false);
					radiobtnLabel.setSelected(false);
					txtNearestNeighbor.setEnabled(false);
					txtEpsilon.setEnabled(true);
				}
			});
		}
		return radiobtnEpsilon;
	}

	/**
	 * This method initializes radiobtnKnn	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getRadiobtnKnn() {
		if (radiobtnKnn == null) {
			radiobtnKnn = new JRadioButton();
			
			if((laplacian.getOption()).equals(LaplacianScore.nearestNeighbor))
			{
				radiobtnKnn.setSelected(true);
			}
			radiobtnKnn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					radiobtnEpsilon.setSelected(false);
					radiobtnKnn.setSelected(true);
					radiobtnLabel.setSelected(false);
					txtNearestNeighbor.setEnabled(true);
					txtEpsilon.setEnabled(false);
				}
			});
		}
		return radiobtnKnn;
	}

	/**
	 * This method initializes btnOk	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton();
			btnOk.setText("    Ok    ");
			btnOk.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(radiobtnEpsilon.isSelected())
					{
						laplacian.setOption(LaplacianScore.epsilon);
						String tmp = txtEpsilon.getText();
						try {
							laplacian.setEps(Double.parseDouble(tmp));
						} catch (NumberFormatException e1) {
							JOptionPane.showMessageDialog(me, "Incorrect data!", "Error", 0);
							return;
						}
					} else if(radiobtnKnn.isSelected()){
						laplacian.setOption(LaplacianScore.nearestNeighbor);
						String tmp = txtNearestNeighbor.getText();
						try {
							laplacian.setKnn(Integer.parseInt(tmp));
						} catch (NumberFormatException e1) {
							JOptionPane.showMessageDialog(me, "Incorrect data!", "Error", 0);
							return;
						}
					} else if(radiobtnLabel.isSelected()){
						laplacian.setOption(LaplacianScore.labelInfo);
					}

					String tmp = txtT.getText();
					try {
						laplacian.setT(Double.parseDouble(tmp));
					} catch (NumberFormatException e1) {
						laplacian.setT(0);
					}
					dispose();
				}
			});
		}
		return btnOk;
	}

	/**
	 * This method initializes pnlButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlButtons() {
		if (pnlButtons == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(5);
			pnlButtons = new JPanel();
			pnlButtons.setLayout(flowLayout);
			pnlButtons.add(getBtnOk(), null);
			pnlButtons.add(getBtnCancel(), null);
		}
		return pnlButtons;
	}

	/**
	 * This method initializes btnCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setText("Cancel");
			btnCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return btnCancel;
	}

	/**
	 * This method initializes pnlMain	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlMain() {
		if (pnlMain == null) {
			pnlMain = new JPanel();
			pnlMain.setLayout(new BorderLayout());
			pnlMain.add(getPnlButtons(), BorderLayout.SOUTH);
			pnlMain.add(getJContentPane(), BorderLayout.CENTER);
		}
		return pnlMain;
	}

	/**
	 * This method initializes radiobtnLabel	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getRadiobtnLabel() {
		if (radiobtnLabel == null) {
			radiobtnLabel = new JRadioButton();

			if((laplacian.getOption()).equals(LaplacianScore.labelInfo))
			{
				radiobtnLabel.setSelected(true);
			}
			radiobtnLabel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					radiobtnEpsilon.setSelected(false);
					radiobtnKnn.setSelected(false);
					radiobtnLabel.setSelected(true);
					txtNearestNeighbor.setEnabled(false);
					txtEpsilon.setEnabled(false);
				}
			});
		}
		return radiobtnLabel;
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"